package com.yuelan.codelib.utils;

import android.content.Context;
import android.content.SharedPreferences;

public class SharedPreferencesUtil {
	private static final String FILENAME = "DMKJ";

	private static String getFileName(Context context) {
		return context.getPackageName() + FILENAME;
	}

	public static String getString(Context context, String key, String defValue) {
		return getSharedPreferences(context).getString(key, defValue);
	}

	public static void putString(Context context, String key, String value) {
		SharedPreferences.Editor edit = getSharedPreferences(context).edit();
		edit.putString(key, value);
		edit.commit(); // 保存数据信息
	}

	public static int getInt(Context context, String key, int defValue) {
		return getSharedPreferences(context).getInt(key, defValue);
	}

	public static void putInt(Context context, String key, int value) {
		SharedPreferences.Editor edit = getSharedPreferences(context).edit();
		edit.putInt(key, value);
		edit.commit(); // 保存数据信息
	}

	public static long getLong(Context context, String key, long defValue) {
		return getSharedPreferences(context).getLong(key, defValue);
	}

	public static void putLong(Context context, String key, long value) {
		SharedPreferences.Editor edit = getSharedPreferences(context).edit();
		edit.putLong(key, value);
		edit.commit(); // 保存数据信息
	}

	public static boolean getBoolean(Context context, String key,
			boolean defValue) {
		return getSharedPreferences(context).getBoolean(key, defValue);
	}

	public static void putBoolean(Context context, String key, boolean value) {
		SharedPreferences.Editor edit = getSharedPreferences(context).edit();
		edit.putBoolean(key, value);
		edit.commit(); // 保存数据信息
	}

	private static SharedPreferences getSharedPreferences(Context context) {
		SharedPreferences sharedPreferences = context.getSharedPreferences(
				getFileName(context), Context.MODE_WORLD_READABLE
						| Context.MODE_MULTI_PROCESS);
		return sharedPreferences;
	}
}
